package com.easy.orm.demo;

import java.sql.Connection;
import java.sql.SQLException;

import com.easy.orm.source.EasyDataSource;

/**
 * 数据源测试
 * 
 * @author kun
 *
 */
public class SourcesDemo {

	/**
	 * 初始化数据源
	 * 
	 * @return 数据源
	 */
	public EasyDataSource initDataSource() {
		EasyDataSource dataSource = new EasyDataSource();
		dataSource.setDriver("org.postgresql.Driver");
		dataSource.setUrl("jdbc:postgresql://192.168.2.119:5432/test1");
		dataSource.setUsername("postgres");
		dataSource.setPassword("123456");
		return dataSource;
	}

	public static void main(String[] args) throws SQLException {

		SourcesDemo demo = new SourcesDemo();
		EasyDataSource datasource = demo.initDataSource();

		// 单一持有数据源
		for (int a = 0; a < 5; a++) {
			Connection connection = datasource.getConnection();
			System.out.println(connection);
			connection.close();
		}
		System.out.println(datasource.getSize());

		// 持有多个数据源
		Connection[] connectionArray = new Connection[8];
		for (int a = 0; a < connectionArray.length; a++) {
			connectionArray[a] = datasource.getConnection();
			System.out.println(connectionArray[a]);
		}
		System.out.println(datasource.getSize());
		for (int a = 0; a < connectionArray.length; a++) {
			System.out.println(connectionArray[a]);
			connectionArray[a].close();
		}
		System.out.println(datasource.getSize());
	}

}
